package com.quxian.chambercommerce.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.quxian.chambercommerce.Exception.PasswordInvalidException;
import com.quxian.chambercommerce.Exception.StaffNameNotOnlyException;
import com.quxian.chambercommerce.Exception.UserNotFoundException;
import com.quxian.chambercommerce.bean.AuthorityEntity;
import com.quxian.chambercommerce.bean.MemberInfoEntity;
import com.quxian.chambercommerce.bean.StaffAuthEntity;
import com.quxian.chambercommerce.bean.StaffInfoEntity;
import com.quxian.chambercommerce.dao.LoginDao;
import com.quxian.chambercommerce.dao.StaffAuthDao;
import com.quxian.chambercommerce.service.LoginService;

public class LoginServiceImpl implements LoginService{
	private LoginDao loginDao;
	private StaffAuthDao staffAuthDao;
	
	public void setLoginDao(LoginDao loginDao) {
		this.loginDao = loginDao;
	}

	public void setStaffAuthDao(StaffAuthDao staffAuthDao) {
		this.staffAuthDao = staffAuthDao;
	}

	public StaffInfoEntity login(String loginName, String password) throws Exception {
		StaffInfoEntity staffInfoEntity = loginDao.login(loginName);
		if(staffInfoEntity == null) {
			throw new UserNotFoundException(UserNotFoundException.DEFAULT_MSG);
		}else if(!password.equals(staffInfoEntity.getPassword())){
			throw new PasswordInvalidException(PasswordInvalidException.DEFAULT_MSG);
		}else{
			return staffInfoEntity;
		}
		
	}

	@Override
	public MemberInfoEntity memberLogin(String loginName,String password) throws Exception {
		MemberInfoEntity memberInfoEntity = loginDao.memberLogin(loginName);
		if(memberInfoEntity == null){
			throw new UserNotFoundException(UserNotFoundException.DEFAULT_MSG);
		}else if(!password.equals(memberInfoEntity.getPassword())){
			throw new PasswordInvalidException(PasswordInvalidException.DEFAULT_MSG);
		}else{
			return memberInfoEntity;
		}
		
	}

	@Override
	public void registerStaff(StaffInfoEntity staff) throws Exception {
		Integer count = loginDao.checkStaffOnly(staff.getUser_name());
		if(count>0){
			throw new StaffNameNotOnlyException(StaffNameNotOnlyException.DEFAULT_MSG);
		}else{
			Integer staffId = loginDao.registerStaff(staff);
			Integer[] auths = staff.getAuths();
			if(auths!=null){
				for(int i=0;i<auths.length;i++){
					StaffAuthEntity entity = new StaffAuthEntity();
					entity.setAuth_id(auths[i]);
					entity.setStaff_id(staffId);
					staffAuthDao.addAuth(entity);
				}
			}
		}
		
	}

	@Override
	public Integer registerMember(MemberInfoEntity member) throws Exception {
		return loginDao.registerMember(member);
	}

	@Override
	public void updateStaffAuth(Map<String, Object> map) throws Exception {
		int staffId = (int) map.get("staffId");
		
		Map<String,Object> tempMap = new HashMap<String,Object>();
		tempMap.put("staff_id", map.get("staffId"));
		staffAuthDao.deleteAuth(tempMap);
		
		Integer[] newAuth = (Integer[]) map.get("auth");
		if(newAuth!=null){
			for(int i=0;i<newAuth.length;i++){
				StaffAuthEntity entity = new StaffAuthEntity();
				entity.setAuth_id(newAuth[i]);
				entity.setStaff_id(staffId);
				staffAuthDao.addAuth(entity);
			}
		}
	}

	@Override
	public Integer checkStaffOnly(String userName) {
		return loginDao.checkStaffOnly(userName);
	}

	@Override
	public List<StaffInfoEntity> getAllStaff() throws Exception {
		return loginDao.getAllStaff();
	}

	@Override
	public void deleteStaffById(Integer staffId) {
		loginDao.deleteStaffById(staffId);		
	}

	@Override
	public StaffInfoEntity getStaffById(Integer staffId) {
		return loginDao.getStaffById(staffId);
	}
}
